/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uploadfile;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextBox;
import javafx.scene.input.MouseEvent;
import uploadfile.ImageFileChooser;
import java.io.File;
import javafx.io.http.URLConverter;
import java.io.InputStream;
import java.io.FileInputStream;
import javafx.io.http.HttpRequest;
import javafx.io.http.HttpHeader;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.scene.Cursor;
import javafx.scene.text.Text;
import javafx.scene.paint.Color;
import javafx.stage.Alert;
import javafx.scene.control.Button;

/**
 * @author ToanNhan
 */

Stage {
    var percent : Number = 0;
    var fileName : TextBox;
    var progressBar : ProgressBar;
    var textMessage : Label;
    var textXoa : Text;
    var btnBrowse : Button;
    title: "Application title"
    scene: Scene {
        width: 275
        height: 20
        content: [
            fileName = TextBox{
                    columns: 25
                    onMouseClicked: function(e: MouseEvent): Void {
                       var imageFileChooser : ImageFileChooser = ImageFileChooser{};
                       var result : Number = imageFileChooser.showOpenDialog(null);
                       if (result == ImageFileChooser.APPROVE_OPTION){
                           var file : File = imageFileChooser.getSelectedFile();
                           var url = "http://localhost:8080/WebsiteBanHang/ThemSanPham";
                           var urlConverter: URLConverter = URLConverter{};
                            var encodingSevletUrl: String = urlConverter.encodeURL(
                                "{url}?tenTapTin={file.getName()}&command=uploadHinhAnh"
                            );
                            var is: InputStream = new FileInputStream(file);
                            var httpRequest: HttpRequest = HttpRequest{
                                location: encodingSevletUrl
                                method: HttpRequest.POST
                                headers: [
                                    HttpHeader{
                                        name: HttpHeader.CONTENT_TYPE
                                        value: "image/file"
                                    }
                                ]
                                source:is
                                onWriting: function(){
                                        btnBrowse.visible = false;
                                        fileName.visible = false;
                                        progressBar.visible = true;
                                    }
                                onWritten: function(num : Long){
                                        percent = (num / file.length());
                                    }

                                onDone: function(){
                                    textXoa.visible = true;
                                    progressBar.visible = false;
                                    textMessage.text = file.getName();
                                    textMessage.visible = true;
                                    btnBrowse.visible = false;
                                }
                            }
                            httpRequest.start();
                       }

                    }
                }
            btnBrowse = Button{
                    layoutX: 200
                    text: "Browse.."
                    onMouseClicked: function(e: MouseEvent): Void {
                        var imageFileChooser : ImageFileChooser = ImageFileChooser{};
                       var result : Number = imageFileChooser.showOpenDialog(null);
                       if (result == ImageFileChooser.APPROVE_OPTION){
                           var file : File = imageFileChooser.getSelectedFile();
                           var url = "http://localhost:8080/WebsiteBanHang/ThemSanPham";
                           var urlConverter: URLConverter = URLConverter{};
                            var encodingSevletUrl: String = urlConverter.encodeURL(
                                "{url}?tenTapTin={file.getName()}&command=uploadHinhAnh"
                            );
                            var is: InputStream = new FileInputStream(file);
                            var httpRequest: HttpRequest = HttpRequest{
                                location: encodingSevletUrl
                                method: HttpRequest.POST
                                headers: [
                                    HttpHeader{
                                        name: HttpHeader.CONTENT_TYPE
                                        value: "image/file"
                                    }
                                ]
                                source:is
                                onWriting: function(){
                                        btnBrowse.visible = false;
                                        fileName.visible = false;
                                        progressBar.visible = true;
                                    }
                                onWritten: function(num : Long){
                                        percent = (num / file.length());
                                    }

                                onDone: function(){
                                    textXoa.visible = true;
                                    progressBar.visible = false;
                                    textMessage.text = file.getName();
                                    textMessage.visible = true;
                                    btnBrowse.visible = false;
                                }
                            }
                            httpRequest.start();
                       }
                    }

                }
            progressBar = ProgressBar{
                    progress: bind percent
                    visible: false
                }
            textMessage = Label{
                    width : 150
                    font : Font.font("Arial", 14)
                    visible: false
                }
            textXoa = Text{
                    visible: false
                    layoutX: bind textMessage.width + 15
                    layoutY: 15
                    font: Font.font("Arial", 12)
                    fill: Color.RED
                    content:"Xóa"
                    managed: true
                    cursor: Cursor.HAND
                    onMouseClicked: function(e: MouseEvent): Void {
                        var result: Boolean = Alert.question("Bạn có muốn xóa file này?");
                        if (result == true){
                            println("vao rui ne");
                            var url = "http://localhost:8080/WebsiteBanHang/ThemSanPham";
                            var urlConverter: URLConverter = URLConverter{};
                            var encodingSevletUrl: String = urlConverter.encodeURL(
                                "{url}?command=xoaHinhAnh"
                            );
                            var httpRequest: HttpRequest = HttpRequest{
                                location: encodingSevletUrl
                                method: HttpRequest.GET
                                headers: [
                                    HttpHeader{
                                        name: HttpHeader.CONTENT_TYPE
                                        value: "text/html"
                                    }
                                ]                                
                                onDone: function(){
                                    progressBar.visible = false;
                                    textMessage.visible = false;
                                    textXoa.visible = false;
                                    fileName.visible = true;
                                    btnBrowse.visible = true;
                                    percent = 0;
                                }
                            }
                            httpRequest.start();
                        }

                    }


                }
        ]
        
    }

}